package com.cwd.arm.domain.repository.impl;

import com.cwd.arm.common.exception.ArmException;
import com.cwd.arm.domain.entity.AuthEntity;
import com.cwd.arm.infrastructure.dao.AuthDao;
import com.cwd.arm.infrastructure.dao.RoleAuthDao;
import com.cwd.arm.infrastructure.dao.RoleDao;
import com.cwd.arm.infrastructure.dao.UserRoleDao;
import com.cwd.arm.infrastructure.po.AuthPo;
import com.cwd.arm.infrastructure.po.RoleAuthPo;
import com.cwd.arm.infrastructure.po.RolePo;
import com.cwd.arm.domain.entity.RoleEntity;
import com.cwd.arm.domain.repository.RoleRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Description:角色
 * Created on 2021年11月16日
 *
 * @author yan
 */
@Repository
public class RoleRepositoryImpl implements RoleRepository {

    @Autowired
    private RoleDao dao;

    @Autowired
    private RoleAuthDao roleAuthDao;

    @Autowired
    private AuthDao authDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public void save(RoleEntity roleEntity) {
        dao.save(convertToRolePo(roleEntity));
    }

    @Override
    public void update(RoleEntity roleEntity) {
        if (!roleEntity.canUpdate()) {
            throw new ArmException("不可以修改管理员角色");
        }
        dao.updateById(convertToRolePo(roleEntity));
        //全量更新用户的权限信息
        Long roleId = roleEntity.getId();
        roleAuthDao.removeByRoleId(roleEntity.getId());
        if (roleEntity.getBtnAuthList() != null) {
            updateRoleAuth(roleEntity.getBtnAuthList(), roleId);
        }
        if (roleEntity.getMenuAuthList() != null) {
            updateRoleAuth(roleEntity.getMenuAuthList(), roleId);
        }
    }

    @Override
    public void del(Long id) {
        RoleEntity roleEntity = buildEntity(id);
        if(roleEntity.canDel()){
            roleAuthDao.removeByRoleId(id);
            userRoleDao.removeByRoleId(id);
            dao.removeById(id);
        }
    }

    private void updateRoleAuth(List<AuthEntity> authEntityList, Long roleId) {
        for(AuthEntity authEntity : authEntityList){
            RoleAuthPo roleAuthPo = new RoleAuthPo();
            roleAuthPo.setRoleId(roleId);
            if(authEntity.getKey()!=null){
                roleAuthPo.setAuthKey(authEntity.getKey());
            }else{
                AuthPo authPo = authDao.getById(authEntity.getId());
                roleAuthPo.setAuthKey(authPo.getKey());
            }
            roleAuthDao.save(roleAuthPo);
        }
    }

    private RolePo convertToRolePo(RoleEntity entity) {
        RolePo RolePo = new RolePo();
        BeanUtils.copyProperties(entity, RolePo);
        return RolePo;
    }

    private RoleEntity buildEntity(Long id){
        RoleEntity roleEntity = new RoleEntity();
        RolePo rolePo = dao.getById(id);
        BeanUtils.copyProperties(rolePo,roleEntity);
        return roleEntity;
    }
}
